package dao;

import util.Time;
import util.db.MySqlConn;
import factory.AkimimiOrderFactory;

public class Order {

	public static void MappingOrders()
	{
		for(AkimimiOrderFactory orders: AkimimiOrder.getAkimimiOrderList())
		{
			int a_o_id = orders.getId();
			String userid = orders.getUser_id();
			String agentid = orders.getAgent_member_id();
			//String createid = orders.ge();
			String modifyid = orders.getUpdate_id();
			
			int user_id = AkimimiOrder.GetIdByName(userid);

			int agent_id = AkimimiOrder.GetIdByName(agentid);

			int create_id = 0;
			
			int modify_id = AkimimiOrder.GetIdByName(modifyid);
			
			if(user_id > 0)
			{
				//TODO:insert orders
				int e_o_id = InsertOrder(orders, user_id, agent_id, create_id, modify_id);
				//TODO:insert order_items
				OrderItem.MappingOrderItems(a_o_id);
				if(e_o_id > 0)
				{
					//TODO:insert Akimimi_order_mappings
					if(AkimimiOrder.InsertOrderMappingLog(a_o_id, e_o_id) > 0)
					{
						System.out.println("[" + Time.nowTime() + "][" + userid + "]: " + " ~_~ OK !");
					}
					else
					{
						System.out.println("[" + Time.nowTime() + "][" + userid + "]: " + " @_@ insert mappings error !");
					}
				}
				else
				{
					System.out.println("[" + Time.nowTime() + "][" + userid + "]: " + " @_@ intsert orders error ......!");
				}
			}
			else
			{
				System.out.println("[" + Time.nowTime() + "][" + userid + "]: " + " @_@ don't get user_id ......!");
			}
		}
	}
	
	public static int InsertOrder(Object orders, int user_id, int agent_id, int create_id, int modify_id)
	{
		AkimimiOrderFactory order = (AkimimiOrderFactory) orders;
		
		String CouponName = "";
		String couponValue = "";
		if(order.getCoupon_name()!= null || order.getCoupon_name().equals(""))
		{
			CouponName = order.getCoupon_name();
		}
		if(order.getCoupon_value() != 0)
		{
			couponValue = String.valueOf(order.getCoupon_value());
		}
		String remark = CouponName + couponValue + order.getRemark();
		
		String sql = "insert into orders(id, order_num, user_id, status," +
				"shipping_fee, discount_fee, tax_fee," +
				"merchandise_total, total, ship_profit_total, order_profit_total," +
				"shipping_expect, shipping_time, payment_method, invoice," +
				"agent_campus_id, agent_id, delivery_phone1, delivery_phone2," +
				"delivery_address_1, delivery_address_2, delivery_address_3," +
				"delivery_detail_address, delivery_email, remark, is_returned," +
				"modified, modify_id, created, create_id) values (" +
				order.getId() + ",'" + order.getOut_trade_no() + "'," + user_id + "," + order.getStatus() + "," + 
				order.getDelivery_cost() + "," + order.getOther_cost() + "," + order.getCoupon_value() + "," + 
				order.getOrder_cost() + "," + order.getOrder_cost() + "," + 0 + "," + 0 + "," + 
				order.getDelivery_mode() + "," + 0 + "," + order.getPay_mode() + ",'" + order.getInvoice() + "'," + 
				order.getAgent_area_id() + "," + agent_id + ",'" + order.getDelivery_phone_1() + "','" + order.getDelivery_phone_2() + "'," + 
				order.getDelivery_address_1() + "," + order.getDelivery_address_2() + "," + order.getDelivery_address_3() + ",'" + 
				order.getDelivery_detal_address() + "','" + order.getDelivery_email() + "','" + remark + "'," + 0 + ",'" + 
				order.getUpdate_time() + "'," + modify_id + ",'" + order.getCreate_time() + "'," + create_id + ")"; 
		MySqlConn mysqlConn = new MySqlConn();
		int id = mysqlConn.executeGetid(sql);
		mysqlConn.closeConn();
		return id;
	}
}
